﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace CatClient.Util
{
    internal static class RemoteServer
    {
        public static List<ServerInfo> GetServerInfo(string regAddress, string domain)
        {
            string text = HttpUtils.HttpGet(regAddress + "/remote/dns/get?domain=" + domain + "&token=78db4eb5ae2cd22884f9a6363fed892a55847e61c83c5878a490be73a673fcd23dea802f2d8a10f7ba0e0d8bebdadb2fadf4796a399e521284f98fdfc2f204c8ac7e8a20a9fd61f30ffaffcc6915e40d1e7d7242c332b839b1a200b40a18c3d8aceadcb1ee47f6cbec0b60f692f5a03cc6bc7c11b35f752682e3ad5c9143f12a", null, 10000);
            if (string.IsNullOrEmpty(text))
            {
                return null;
            }
            List<ServerInfo> result;
            try
            {
                RegistryResult expr_33 = new JavaScriptSerializer().Deserialize<RegistryResult>(text);
                result = ((expr_33 != null) ? expr_33.Data : null);
            }
            catch
            {
                result = null;
            }
            return result;
        }

        public static string ToServerString(this List<ServerInfo> servers)
        {
            string[] array = new string[servers.Count];
            int num = 0;
            foreach (ServerInfo current in servers)
            {
                List<PortInfo> expr_25 = current.PortInfos;
                string arg = (expr_25 != null && expr_25.Count == 1) ? current.PortInfos[0].Port : "2280";
                array[num++] = string.Format("{0}:{1}", current.ServerIp, arg);
            }
            return string.Join(",", array);
        }
    }
}
